package com.business.controller;

import com.business.domain.CarBrand;
import com.business.domain.Loan;
import com.business.domain.LoanInterest;
import com.business.service.CarService;
import com.business.service.LoanService;
import com.sys.Utils.DbConstants;
import com.sys.dao.DictItemMapper;
import com.sys.domain.DictItem;
import com.sys.domain.DictItemExample;
import com.sys.domain.DictItemExample.Criteria;
import com.sys.result.PageParam;
import com.sys.result.RequestPageVo;
import com.sys.result.RequestResultVo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("business/loan")
public class LoanController {

    @Resource
    private LoanService loanService;

    @Resource
    private CarService carService;

    @Resource
    private DictItemMapper dictItemMapper;

    /**
     * 加载车贷产品列表页
     */
    @RequestMapping("toLoanPage")
    public String toLoanPage(Model model) {
        //获取所有的周期
        DictItemExample example = new DictItemExample();
        example.setOrderByClause("item_order");
        Criteria createCriteria = example.createCriteria();
        createCriteria.andDictCodeEqualTo(DbConstants.LOAN_PERIOD);
        //获取所有品牌
        List<CarBrand> brandList = carService.getAllCarBrand();
        model.addAttribute("brandList", brandList);
        List<DictItem> itemList = dictItemMapper.selectByExample(example);
        model.addAttribute("itemList", itemList);
        return "loan/loanPage";
    }

    /**
     * 查询车贷产品列表
     */
    @RequestMapping("queryLoanList")
    public @ResponseBody
    RequestPageVo<Loan> queryLoanList(PageParam param, String productName) {
        return loanService.queryLoanList(param, productName);
    }

    /**
     * 添加或修改车贷产品
     */
    @RequestMapping("saveOrUpdate")
    public @ResponseBody
    RequestResultVo<Loan> saveOrUpdateLoan(Loan loan, String[] limitedBrands, MultipartFile contractFile) {
        RequestResultVo<Loan> result;
        StringBuilder limit = new StringBuilder();
        if (limitedBrands != null && limitedBrands.length > 0) {
            for (String string : limitedBrands) {
                limit.append(string).append(",");
            }
        }
        loan.setLimitedBrands(limit.toString());
        if (loan.getId() != null) {
            result = loanService.updateLoan(loan, contractFile);
        } else {
            result = loanService.addLoan(loan, contractFile);
        }
        return result;
    }

    /**
     * 根据id查询车贷产品
     */
    @RequestMapping("queryLoanById")
    public @ResponseBody
    RequestResultVo<Loan> queryLoanById(Integer id) {
        return loanService.queryLoanById(id);
    }

    /**
     * 删除车贷产品
     */
    @RequestMapping("deleteLoan")
    public @ResponseBody
    RequestResultVo<Loan> deleteLoan(Integer id) {
        return loanService.deleteLoan(id);
    }

    /**
     * 根据id获取利率分期情况
     */
    @RequestMapping("getInterest")
    public @ResponseBody
    RequestPageVo<LoanInterest> getInterest(Integer id) {

        return loanService.getInterest(id);
    }


    @RequestMapping("saveOrUpdateInterest")
    public @ResponseBody
    RequestResultVo<LoanInterest> saveOrUpdateInterest(String json, Integer loanId) {

        return loanService.saveOrUpdateInterest(json, loanId);
    }


    @RequestMapping("/exportLoan")
    public void exportLoan(HttpServletResponse res) {
        HSSFWorkbook exportCar = loanService.exportLoan();
        try {
            res.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("车贷库数据.xls", "UTF-8"));
            ServletOutputStream out = res.getOutputStream();
            exportCar.write(out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




